package top.itpath.admin.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.itpath.admin.RequirePermission;
import top.itpath.admin.entity.SysMenu;
import top.itpath.admin.service.ISysMenuService;
import top.itpath.common.Result;
import top.itpath.common.util.QueryTreeUtil;

import java.util.List;

/**
 * 系统/菜单
 *
 * @author zhaomengru
 * @time 2023/10/13 - 22:49
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/menu")
public class MenuController {

    private final ISysMenuService menuService;

    /**
     * 获取完整菜单树
     */
    @GetMapping("/tree")
    Result<?> tree() {
        List<SysMenu> list = menuService.list();
        return Result.success(QueryTreeUtil.queryTree(list, null));
    }

    /**
     * 添加菜单
     */
    @PostMapping("/add")
    @RequirePermission("menu:add")
    Result<?> add(@RequestBody SysMenu sysMenu) {
        menuService.save(sysMenu);
        return Result.success(sysMenu);
    }

    /**
     * 更新菜单
     */
    @PostMapping("/update")
    @RequirePermission("menu:update")
    Result<?> update(@RequestBody SysMenu sysMenu) {
        menuService.updateById(sysMenu);
        return Result.success(sysMenu);
    }

    /**
     * 删除菜单
     */
    @PostMapping("/delete")
    @RequirePermission("menu:delete")
    Result<?> delete(@RequestParam Long id) {
        menuService.removeById(id);
        return Result.success();
    }
}


